package freelinz.smart.gwtex.client.ui;


import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Widget;

import freelinz.smart.gwtex.client.ui.SmartKitsAll.Resized;
import freelinz.smart.gwtex.client.ui.SmartKitsAll.SmartKitI;
import freelinz.smart.gwtex.client.ui.SmartKitsAll.SpliterI;
import freelinz.smart.gwtex.client.ui.SmartKitsAll.SpliterI.SpliterType;
import freelinz.smart.gwtex.user.client.DOMEx;

public class SmartKitsH extends AbstractSmartKits
{
    public SmartKitsH()
    {
        super();
        final FlowPanel absolutePanel = new FlowPanel();
        Resized rt = new Resized(){
            public Widget getWidget()
            {
                return absolutePanel;
            }
            public void resized(int width, int height)
            {
                DOMEx.setOffsetMarginSize(widget.getElement(), width, height);
            }
        };
        absolutePanel.setStyleName("SmartKits");
//        DOMEx.setStyleAttributeEx(absolutePanel.getElement(), "clear", "both");
        DOMEx.setStyleAttributeEx(absolutePanel.getElement(), "float", "left");

        this.addResizedWidget(rt);
        this.addResizedListener(rt);
    }

    public void addSmartKit(SmartKitI one, SmartKitI two, SpliterI spliter)
    {
        boolean addOne = true;

        int size = this.getSmartKitList().size();
        if(size > 0)
        {
//          为空取上次增加的最后一个
            if((one == null))
            {
                one = (SmartKitI)(getSmartKitList().get(size-1));
                addOne = false;
            }
            else if(getSmartKitList().indexOf(one)==(size-1))
            {
                addOne = false;
            }
        }
        
        if(this == one || one == null || //不能为空
           this == two || two == null || //不能为空
           this == spliter || spliter == null || //不能为空
           one == two || //不能相等
           getSmartKitList().contains(two) || //不能忝加已忝加的对象
           (addOne && getSmartKitList().contains(one)) || //不能忝加已忝加的对象
           this.getSpliterList().contains(spliter) ||
           spliter.getVH() != SpliterType.H)
        {
            throw new IllegalArgumentException("smartKit != null || smartKit != this");
        }
        if(addOne)
        {
            this.getSmartKitList().add(one);
            one.setParentSmartKits(this);
        }
        this.getSmartKitList().add(two);
        two.setParentSmartKits(this);
        
        this.getSpliterList().add(spliter);
        spliter.setParentSmartKits(this);
        spliter.setOne(one);
        spliter.setTwo(two);
        
        FlowPanel f = (FlowPanel)resizedWidget.getWidget();
        if(addOne)
        {
            one.getWidget().setHeight("100%");
            DOMEx.setStyleAttributeEx(one.getWidget().getElement(), "float", "left");
            DOMEx.setStyleAttributeEx(one.getWidget().getElement(), "overflow", "auto");
            f.add(one.getWidget());
        }
        
        DOMEx.setStyleAttributeEx(spliter.getWidget().getElement(), "float", "left");
        DOMEx.setStyleAttributeEx(one.getWidget().getElement(), "overflow", "auto");
        f.add(spliter.getWidget());
        
        DOMEx.setStyleAttributeEx(two.getWidget().getElement(), "float", "left");
        DOMEx.setStyleAttributeEx(one.getWidget().getElement(), "overflow", "auto");
        two.getWidget().setHeight("100%");
        f.add(two.getWidget());
        int num = this.smartKitList.size();
        String style = (int)(100.0/num)+"%";
        SmartKit sm;
        for(int i = 0; i<num; i++)
        {
            sm = (SmartKit)smartKitList.get(i);
            sm.getWidget().setWidth(style);
        }
        
    }

    public void resized(int width, int height)
    {
        if(this.smartKitList != null && smartKitList.size() > 0)
        {
            Element elem = widget.getElement();
            DOMEx.setOffsetMarginSize(elem, width, height);
            DOMEx.setOffsetMarginSize(resizedWidget.getWidget().getElement(), width, height);
            
            int spW = 0;
            for(int i =0,num=this.spliterList.size();i<num;i++)
            {
                spW += ((SpliterI)spliterList.get(i)).getWidth();
            }
            int num = smartKitList.size();
            int w = (width-DOMEx.getPaddingLeftRight(elem)-spW)/num;
            for(int i=0;i<num;i++)
            {
                SmartKit sm = (SmartKit)this.smartKitList.get(i);
                sm.resized(w, height);
            }
        }
        else
        {
            super.resized(width, height);
        }
    }

    public void resized(SmartKitI smartKitI, int direction, int changeX, int changeY)
    {
        
    }
    public void clear()
    {
        super.clear();
        if(smartKitList != null)this.smartKitList.clear();
        if(spliterList != null) this.spliterList.clear();
    }

    public void addResizedWidget(Resized w)
    {
        super.addResizedWidget(w);
    }
}
